﻿#ifndef UIMODELMANAGEDELEGATE_H
#define UIMODELMANAGEDELEGATE_H

#include <QObject>
#include <QStyledItemDelegate>
#include <shlib/viewmodel/imodel.h>
#include <QPushButton>
#include <QMenu>

class UiSceneMenuBtn : public QPushButton
{
public:
    UiSceneMenuBtn(quint16 id, const QString& name, QWidget* parent):QPushButton(parent),m_id(id),m_name(name){
        auto action = m_menu.addAction("编辑场景");

        connect(action,&QAction::triggered,this,[=](){
            //CSigManager::GetInstance()->signEditScene(m_id);
        });
        action = m_menu.addAction("重命名场景");
        connect(action,&QAction::triggered,this,[=](){
            //CSigManager::GetInstance()->sigRenameScene(m_id);
        });
        action = m_menu.addAction("配置场景模型");
        connect(action,&QAction::triggered,this,[=](){
            //CSigManager::GetInstance()->sigConfigSceneModel(m_id,0);
        });
        action = m_menu.addAction("复制场景");
        connect(action,&QAction::triggered,this,[=](){
            //CSigManager::GetInstance()->sigCopyScene(m_id);
        });
        action = m_menu.addAction("监控场景");
        connect(action,&QAction::triggered,this,[=](){
            //CSigManager::GetInstance()->signMonitoringScene(m_id);
        });
        action = m_menu.addAction("删除场景");
        connect(action,&QAction::triggered,this,[=](){
            //CSigManager::GetInstance()->signRemoveScene(m_id);
        });

        connect(this,&QPushButton::clicked,this,[=](){
            //CSigManager::GetInstance()->sigSceneMenuClicked(id,name);

            if(!m_bInitMenu)
            {
                m_bInitMenu = true;
                //显示-隐藏，让size更新正确
                m_menu.show();
                m_menu.setVisible(false);
                popupMenu();
            }
            else{
                popupMenu();
            }

        });
    }
    void setText(const QString& text){
        QPushButton::setText(text);
    }

private:
    /*
    描述：弹出菜单
    日期: 2024-01-23
    */
    void popupMenu(){
        auto pos = this->mapToGlobal(QPoint(0,0));
        pos.setX(pos.x()-m_menu.width()+this->width()/2);
        pos.setY(pos.y()+this->height()/2);
        m_menu.popup(pos);
    }

private:
    QMenu m_menu;
    quint16 m_id;
    QString m_name;
    bool m_bInitMenu = false;//因为第一次使用菜单宽度不正确，使用标志位检查是否是第一次使用。
};



class UiModelManageDelegate : public QStyledItemDelegate
{
    Q_OBJECT
public:
    explicit UiModelManageDelegate(QObject *parent = nullptr);

    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;

    bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);

    QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;

    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;

    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
signals:

protected:
    /*
    描述：获得菜单矩形位置
    日期: 2024-01-23
    */
    QRect getMenuRect(const QStyleOptionViewItem &option) const;

private:
    const int m_itemWidth = 145;                                  //item宽度
    const int m_itemHeight =170;                                  //item高度
    const QSize m_imageSize = QSize(145,145);                     //封面大小
    const int m_hSpacing = 3;                                    //封面水平间距
    const int m_vSpacing = 3;                                    //封面垂直间距
    const int m_textSpacing = m_vSpacing/2;                       //文字-封面间距
    const QSize m_menuSize = QSize(20,20);                        //菜单按钮大小
    class QListView* m_view = nullptr;
};

#endif // UIMODELMANAGEDELEGATE_H
